package components.dialogs
{
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_hscrollArea;
	import UIFree.uif_component;
	
	import classes.clt_events;
	
	import components.clt_box;
	import components.clt_collagesBox;
	import components.game.clt_collageItem;
	import components.pj_hscrollArea;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class clt_collages extends clt_dialog {
		private var _rows	: int = 1;
		private var _items	: Array;
		private var _list	: uif_hscrollArea;
		private var _box	: clt_collagesBox;
		private var _module	: Object;
		
		public function clt_collages(parent:uif_component, a_width:int=610, a_height: int=160) {
			super(parent, (parent.width - a_width) / 2, parent.height - a_height * 1.8, a_width, a_height, false, 1);
		}
		
		override protected function createDialogComponents():void {
			super.createDialogComponents();
			var inner: Rectangle = getInner();
			
			se_util.defButton(this, (inner.width - se_util.DEFWIDTHBUTTON) / 2, inner.height - se_util.DEFHEIGHTBUTTON, clt.classes.locale.CLOSE, function(e: Event): void {
				close();
			});
		}
		
		private function doItemSelect(e: ObjectEvent): void {
			workspace.dispatchEvent(new ObjectEvent(clt_events.SHOWGAMEITEM, _box.itemsLayer.selectItem.control));
		}
		
		protected function createBox(): void {
			if (_box) _box.dispose();
			
			var inner: Rectangle = getInner();
			_box = new clt_collagesBox(this, _module, inner.x, inner.y, inner.width, inner.height - se_util.DEFHEIGHTBUTTON, 0, 'tabBox', false);
			_box.addEvent(Event.SELECT, doItemSelect);
			_box.tabs.selectIndex = 0;
		}
		
		public function showCollages(collages: Array): void {

			_module = {groups:[]};
			
			var isMyAlbum: Boolean = false;
			var isGameList: Boolean = false;
			collages.forEach(function(item: Object, i: int, arr: Array): void {
				if (collages[i].group == 0) isGameList = true;
				if (collages[i].group == 1) isMyAlbum = true;
			});
			
			if (isGameList) _module.groups.push({
				id: 0,
				name: 'Конкурсные'
			});
			
			if (isMyAlbum) _module.groups.push({
				id	: 1,
				name: 'Альбом'
			});
			
			_module.list = collages;
			refreshCollages();
			show();
		}		
		
		public function refreshCollages(): void {
			createBox();
		}
	}
}